package com.zdft.promotion.common.util;

import org.apache.commons.lang3.StringUtils;

import java.net.*;
import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * restful接口返回结果
 */
public class RestfulResult {
    /**
     * 返回码：请求成功
     */
    public static final String SUCCESS = "0";

    private static ResourceBundle bundle;

    private static String address = null;

    private static String appCode;

    static {
        try {
            bundle = ResourceBundle.getBundle("i18n/error_message", Locale.SIMPLIFIED_CHINESE);
            address = getLocalHostIp();
            appCode = bundle.getString("APP_CODE");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private final String code;
    private final String message;
    private final Object object;

    /**
     * 成功结果构造方法
     */
    public RestfulResult() {
        this(null);
    }

    /**
     * 成功结果构造方法
     *
     * @param object 参数
     */
    public RestfulResult(Object object) {
        this(SUCCESS, "success", object);
    }

    /**
     * 失败结果构造方法
     *
     * @param code    结果编码
     * @param message 结果信息
     */
    public RestfulResult(String code, String message) {
        this(code, message, null);
    }

    /**
     * 结果构造方法
     *
     * @param code    结果编码
     * @param message 结果信息
     * @param object  参数
     */
    public RestfulResult(String code, String message, Object object) {
        this.code = code;
        this.object = object;

        if (null != bundle && bundle.containsKey(code)) {
            if ("500".equals(code) || "SYS_FAULT".equals(code) || "-1".equals(code)) {
                this.message = bulidProjectInfo(code);
            } else {
                this.message = bundle.getString(code);
            }
        } else {
            this.message = message;
        }
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Object getObject() {
        return object;
    }

    private String bulidProjectInfo(String code) {
        StringBuffer sb = new StringBuffer();
        sb.append(appCode == null ? "WAREHOUSE" : appCode);
        if (StringUtils.isNotEmpty(address)) {
            sb.append("(").append(address).append(")");
        }
        sb.append(bundle.getString(code));
        return sb.toString();
    }

    public static String getLocalHostIp() {
        Enumeration allNetInterfaces;
        String host = "";
        try {
            allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                Enumeration addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = (InetAddress) addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address && !"127.0.0.1".equals(ip.getHostAddress())) {
                        host = ip.getHostAddress();
                        break;
                    }
                }
                if (null != host && !"".equals(host.trim())) {
                    break;
                }
            }
        } catch (SocketException e) {
            host = "";
            e.printStackTrace();
        }
        return host;
    }
}
